<?php

namespace App\Services\BatchCreateImage;

use App\Models\Collection;
use App\Jobs\MakeBatchImageJob;

class BatchCreateImage
{
	protected $collection;

	public function __construct(Collection $collection)
	{
		$this->collection = $collection;
	}


	public function make()
	{
		if(empty($this->collection->materials)){

			return false;
		}

		$materails = $this->collection->materials()->orderBy('is_back')->get();

		$result=null;
		$images = [];
		foreach($materails as $k=>$v){
			$images[$k] =$v->images; 
		}
		//dd($images);
		$this->recursion($images);

		
	}


	function recursion($groups, $echo = '') {
	    $current = array_pop($groups);
	    $end = empty($groups);

	    $echo .= $echo ? ",":"";
	    foreach($current as $item){
	    	$rEcho = $echo.$item;
	    	if($end){
	    		dispatch(new MakeBatchImageJob(explode(",",$rEcho), $this->collection->id))->onQueue('default');
	    		
	    	}else{

	    		$this->recursion($groups,$rEcho);
	    	}


	    }
	}


}
